package com.konne.nightmare.FastPublicOpinion.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/1/24  Time:下午 2:13.
 * description: ** .
 */
public class QJActionBar extends FrameLayout {
    private View viewBg;
    private boolean isOpenImmersesBar = true;
    private int bgColor;
    private int mHeight;
    public QJActionBar(@NonNull Context context) {
        this(context, null);
    }

    public QJActionBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QJActionBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View action = LayoutInflater.from(context).inflate(R.layout.view_action_bar, null);
        action.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(45)));
        addView(action);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QJActionBar, defStyleAttr, 0);
        String text = a.getString(R.styleable.QJActionBar_qhab_title);
        boolean isLeftBack = a.getBoolean(R.styleable.QJActionBar_ahab_back, true);
        isOpenImmersesBar = a.getBoolean(R.styleable.QJActionBar_ahab_immersesBar, true);
        bgColor=a.getColor(R.styleable.QJActionBar_ahab_bgColor,Color.WHITE);
        a.recycle();
        if (isOpenImmersesBar) {
            setImmersesBar();
        }
        if (isLeftBack) {
            setBackImg();
        }
        if (!StringUtils.isEmpty(text)) {
            setTitle(text);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)+(isOpenImmersesBar?BarUtils.getStatusBarHeight():0));
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = specSize;
        if (specMode == MeasureSpec.AT_MOST) {
            result = ScreenUtils.getAppScreenWidth();
        } else {
            result = specSize;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = specSize;
        if (specMode == MeasureSpec.AT_MOST) {

            if(mHeight==0){
                int defHeight = SizeUtils.dp2px(45);
                mHeight=defHeight;
            }
        } else if (specMode == MeasureSpec.EXACTLY) {

            if(mHeight==0){
                mHeight=specSize;
                View action=getChildAt(0);
                LayoutParams params= (LayoutParams) action.getLayoutParams();
                params.height=specSize;
                action.setLayoutParams(params);
            }
        }else {
            if(mHeight==0){
                mHeight=specSize;
            }
        }
        return mHeight;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view == viewBg) {
                view.layout(0, 0, getWidth(), getHeight());
            } else {
                view.layout(0, isOpenImmersesBar ? BarUtils.getStatusBarHeight() : 0, getWidth(), getHeight());
            }
        }
    }

    public ImageView getImgView(@DrawableRes int id) {
        ImageView imageView = new ImageView(getContext());
        imageView.setImageResource(id);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);
        return imageView;
    }

    private TextView getTextView(CharSequence txt) {
        TextView textView = new TextView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        textView.setText(txt);
        textView.setTextColor(0xff222222);
        return textView;
    }

    public View getViewBg() {
        return viewBg;
    }

    public TextView setTitle(CharSequence txt) {
        ViewGroup vp = getCenterViewGroup();
        TextView title;
        if (vp.getChildCount() > 0 && vp.getChildAt(0) instanceof TextView) {
            title = (TextView) vp.getChildAt(0);
            title.setText(txt);
        } else {
            title=getTextView(txt);
            vp.addView(title);
        }
        return title;
    }

    public void setLeftImg(@DrawableRes int id) {
        ViewGroup leftVp = getLeftViewGroup();
        ImageView leftImg = getImgView(id);
        if (leftVp.getChildCount() > 0) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) leftImg.getLayoutParams();
            params.leftMargin = SizeUtils.dp2px(10);
        }
        leftVp.addView(leftImg);
    }

    public ImageView setBackImg() {
        ViewGroup leftVp = getLeftViewGroup();
        leftVp.removeAllViews();
        ImageView leftImg = getImgView(R.drawable.ic_square_back);
        leftImg.setLayoutParams(new LinearLayout.LayoutParams(SizeUtils.dp2px(40), SizeUtils.dp2px(40)));
        leftImg.setImageTintList( ColorStateList.valueOf(0xff3396FE));
        int pad = SizeUtils.dp2px(8);
        leftImg.setPadding(pad, pad, pad, pad);
        leftVp.addView(leftImg);

        leftImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getContext() instanceof Activity) {
                    ((Activity) getContext()).finish();
                }
            }
        });
        return leftImg;
    }

    public void setImmersesBar() {
        int sbHeight = BarUtils.getStatusBarHeight();
        viewBg = new View(getContext());
        viewBg.setBackgroundColor(bgColor);
        addView(viewBg, 0, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        View view = getChildAt(1);
        FrameLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.topMargin = sbHeight;
        view.setLayoutParams(params);
    }

    public ViewGroup getLeftViewGroup() {
        return findViewById(R.id.ll_left);
    }

    public ViewGroup getCenterViewGroup() {
        return findViewById(R.id.ll_center);
    }

    public ViewGroup getRightViewGroup() {
        return findViewById(R.id.ll_right);
    }
}
